package com.sqfw.project.wpReport.controller;

import com.sqfw.common.utils.StringUtils;
import com.sqfw.common.utils.poi.ExcelUtil;
import com.sqfw.framework.aspectj.lang.annotation.Log;
import com.sqfw.framework.aspectj.lang.enums.BusinessType;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.framework.web.page.TableDataInfo;
import com.sqfw.project.system.domain.SysUser;
import com.sqfw.project.system.service.ISysUserService;
import com.sqfw.project.wpReport.domain.WpInfoDetail;
import com.sqfw.project.wpReport.service.IWpInfoDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 工单明细表Controller
 *
 * @author lx
 * @date 2023-08-02
 */
@RestController
@RequestMapping("/wpReport/wpInfoDetail")
public class WpInfoDetailController extends BaseController {

    @Autowired
    private IWpInfoDetailService wpInfoDetailService;

    @Autowired
    private ISysUserService sysUserService;

    @PreAuthorize("@ss.hasPermi('getWpInfoDetailList')")
    @GetMapping("/getWpInfoDetailList")
    public TableDataInfo getWpInfoDetailList(WpInfoDetail wpInfoDetail)
    {
        if (StringUtils.isNotEmpty(wpInfoDetail.getNickName())){
            String nickName = wpInfoDetail.getNickName();
            SysUser sysUser = new SysUser();
            sysUser.setNickName(nickName);
            List<SysUser> sysUsers = sysUserService.selectUserList(sysUser);
            if (!StringUtils.isNotEmpty(sysUsers)){
                List<WpInfoDetail> wpInfoDetails = new ArrayList<>();
                return getDataTable(wpInfoDetails);
            }
            List<String> collect = sysUsers.stream().map(SysUser::getCode).collect(Collectors.toList());
            wpInfoDetail.setCodes(collect);

        }

        if (StringUtils.isNotEmpty(wpInfoDetail.getVisitCode())){
            String visitNickName = wpInfoDetail.getVisitCode();
            SysUser sysUser = new SysUser();
            sysUser.setNickName(visitNickName);
            List<SysUser> sysUsers = sysUserService.selectUserList(sysUser);
            List<String> collect = sysUsers.stream().map(SysUser::getCode).collect(Collectors.toList());
            wpInfoDetail.setVisitCodes(collect);

        }
        startPage();
        Map<String, Object> map = wpInfoDetailService.getWpInfoDetailList(wpInfoDetail);
        List<String> wpids = (List<String>)map.get("wpids");
        List<WpInfoDetail> wpInfoDetails = (List<WpInfoDetail>)map.get("wpInfoDetails");
        return getDataTable(wpInfoDetails,wpids);
    }

    /**
     * 导出工单明细表
     */
    @Log(title = "导出工单明细表", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('wpInfoDetail:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, WpInfoDetail wpInfoDetail) {

        if (StringUtils.isNotEmpty(wpInfoDetail.getNickName())){
            String nickName = wpInfoDetail.getNickName();
            SysUser sysUser = new SysUser();
            sysUser.setNickName(nickName);
            List<SysUser> sysUsers = sysUserService.selectUserList(sysUser);
            if (!StringUtils.isNotEmpty(sysUsers)){
                return ;
            }
            List<String> collect = sysUsers.stream().map(SysUser::getCode).collect(Collectors.toList());
            wpInfoDetail.setCodes(collect);
        }
       /* Map<String, Object> map = wpInfoDetailService.getWpInfoDetailList(wpInfoDetail);
        List<WpInfoDetail> list = (List<WpInfoDetail>)map.get("wpInfoDetails");
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setIndex(i+1);
        }*/

        List<WpInfoDetail> list = wpInfoDetailService.exportWpInfoDetailList(wpInfoDetail);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setIndex(i+1);
        }
        ExcelUtil<WpInfoDetail> util = new ExcelUtil<WpInfoDetail>(WpInfoDetail.class);
        util.exportExcel(response, list, "工单明细表表数据");
    }
}
